package com.adgox.tiantianbiting.home.programDetail;

import com.adgox.tiantianbiting.bean.CommentBean;
import com.adgox.tiantianbiting.bean.BaseResponseBean;
import com.adgox.tiantianbiting.bean.ProgramChooseBean;
import com.adgox.tiantianbiting.bean.ProgramCostBean;
import com.adgox.tiantianbiting.bean.SubProgramBean;
import com.adgox.tiantianbiting.bean.PageBean;
import com.adgox.tiantianbiting.bean.UserInfo;
import com.adgox.tiantianbiting.net.callback.JsonCallback;
import com.adgox.tiantianbiting.utils.ApiConstant;
import com.adgox.tiantianbiting.utils.DialogUtils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.rae.swift.session.SessionManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by tang on 2018/12/26.
 */

public class ProgramDetailPresenterImpl implements ProgramDetailContract.Presenter {

    private ProgramDetailContract.View mView;
    private int userId = SessionManager.getDefault().getUser() == null ? 0 : ((UserInfo) SessionManager.getDefault().getUser()).getId();

    public ProgramDetailPresenterImpl(ProgramDetailContract.View mView) {
        this.mView = mView;
        mView.setPresenter(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void getProgramInfo(int programId) {
        OkGo.<BaseResponseBean<SubProgramBean>>get(ApiConstant.BASE_URL + ApiConstant.GET_PROGRAM_INFO
                + programId + "/" + userId)
                .execute(new JsonCallback<BaseResponseBean<SubProgramBean>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<SubProgramBean>> response) {
                        if (!"0".equals(response.body().getCode())) {
                            mView.onGetProgramInfoFailed(response.body().getMessage());
                            return;
                        }
                        SubProgramBean bean = response.body().getData();
                        mView.onSetProgramInfo(bean);
                    }
                });
    }

    @Override
    public void getProgramChooseInfo(int programId, int startPoint, int maxPage) {
        OkGo.<BaseResponseBean<PageBean<ProgramChooseBean>>>get(ApiConstant.BASE_URL + ApiConstant.GET_PROGRAM_CHOOSE_INFO + programId)
                .params("sort", 0)
                .params("userId", userId)
                .params("startPoint", startPoint)
                .params("maxPage", maxPage)
                .execute(new JsonCallback<BaseResponseBean<PageBean<ProgramChooseBean>>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<PageBean<ProgramChooseBean>>> response) {
                        DialogUtils.dismiss();
                        if (!"0".equals(response.body().getCode())) {
                            mView.onGetProgramChooseInfoFailed(response.body().getMessage());
                            return;
                        }
                        PageBean<ProgramChooseBean> beanList = response.body().getData();
                        mView.onSetProgramChooseInfo(beanList);
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<PageBean<ProgramChooseBean>>> response) {
                        super.onError(response);
                        DialogUtils.dismiss();
                    }
                });
    }

    @Override
    public void getNext10Cost(int programId, int currentSeason) {
        OkGo.<BaseResponseBean<ProgramCostBean>>get(ApiConstant.BASE_URL + ApiConstant.GET_NEXT_10_COST)
                .params("programId", programId)
                .params("seasonId", currentSeason)
                .execute(new JsonCallback<BaseResponseBean<ProgramCostBean>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<ProgramCostBean>> response) {
                        DialogUtils.dismiss();
                        if (!"0".equals(response.body().getCode())) {
                            mView.onGetNext10CostFailed(response.body().getMessage());
                            return;
                        }
                        ProgramCostBean bean = response.body().getData();
                        mView.onSetNext10Cost(bean);
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<ProgramCostBean>> response) {
                        super.onError(response);
                        DialogUtils.dismiss();
                    }
                });
    }

    @Override
    public void addHistory(String s) {
        OkGo.<BaseResponseBean<String>>post(ApiConstant.BASE_URL + ApiConstant.ADD_COLLECT_AND_HISTORY)
                .upJson(s)
                .execute(new JsonCallback<BaseResponseBean<String>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<String>> response) {

                    }
                });
    }

    @Override
    public void addCollect(String s) {
        OkGo.<BaseResponseBean<String>>post(ApiConstant.BASE_URL + ApiConstant.ADD_COLLECT_AND_HISTORY)
                .upJson(s)
                .execute(new JsonCallback<BaseResponseBean<String>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<String>> response) {
                        DialogUtils.dismiss();
                        if ("0".equals(response.body().getCode())) {
                            mView.onAddCollectSuccess();
                        } else {
                            mView.onAddCollectFailed(response.body().getMessage());
                        }
                    }
                });
    }

    @Override
    public void cancelCollect(String s) {
        OkGo.<BaseResponseBean<String>>post(ApiConstant.BASE_URL + ApiConstant.CANCEL_COLLECT)
                .upJson(s)
                .execute(new JsonCallback<BaseResponseBean<String>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<String>> response) {
                        DialogUtils.dismiss();
                        if ("0".equals(response.body().getCode())) {
                            mView.onCancelCollectSuccess();
                        } else {
                            mView.onCancelCollectFailed(response.body().getMessage());
                        }
                    }
                });

    }

    @Override
    public void buyAll(double balance, int programId) {
        Gson gson = new Gson();
        HashMap<String, Object> map = new HashMap<>();
        map.put("balance", "" + balance);
        map.put("programId", programId);
        map.put("userId", userId);
        String s = gson.toJson(map);
        OkGo.<BaseResponseBean<String>>post(ApiConstant.BASE_URL + ApiConstant.BUY_ALL)
                .upJson(s)
                .execute(new JsonCallback<BaseResponseBean<String>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<String>> response) {
                        DialogUtils.dismiss();
                        if ("0".equals(response.body().getCode())) {
                            mView.onBuyAllSuccess();
                        } else {
                            mView.onBuyAllFailed(response.body().getMessage());
                        }
                    }
                });
    }

    @Override
    public void buyEvery(int programId, ArrayList<Long> buySeasons) {
        String url = ApiConstant.BASE_URL + ApiConstant.BUY_EVERY + "?userId=" + userId + "&programId=" + programId
                + "&seasonIds=";
        for (int i = 0; i < buySeasons.size(); i++) {
            if (i < buySeasons.size() - 1) {
                url = url + buySeasons.get(i) + ",";
            } else {
                url += buySeasons.get(i);
            }
        }
        OkGo.<BaseResponseBean<UserInfo>>get(url)
                .execute(new JsonCallback<BaseResponseBean<UserInfo>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<UserInfo>> response) {
                        if ("0".equals(response.body().getCode()) && response.body().getData() != null) {
                            if (response.body().getData().getDisPrice() >= 0){
                                mView.onBuyEverySuccess();
                            } else {
                                mView.onBuyEveryFailed("余额不足，购买失败");
                            }
                        } else {
                            mView.onBuyEveryFailed(response.body().getMessage());
                        }
                    }
                });

    }

    @Override
    public void getComment(int programId, int startPoint, int maxPage) {
        OkGo.<BaseResponseBean<PageBean<CommentBean>>>get(ApiConstant.BASE_URL+ApiConstant.GET_COMMET)
                .params("programId",programId)
                .params("startPoint",startPoint)
                .params("maxPage",maxPage)
                .execute(new JsonCallback<BaseResponseBean<PageBean<CommentBean>>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<PageBean<CommentBean>>> response) {
                        if ("0".equals(response.body().getCode())){
                            mView.onSetComment(response.body().getData());
                        } else {
//                            mView.onGetCommentFailed(response.body().getMessage()==null?"获取评论失败":response.body().getMessage());
                        }
                    }
                });
    }

    @Override
    public void getSubComment(int discussId) {
        OkGo.<BaseResponseBean<List<CommentBean>>>get(ApiConstant.BASE_URL+ApiConstant.GET_SUB_COMMENT)
                .params("discussId",discussId)
                .execute(new JsonCallback<BaseResponseBean<List<CommentBean>>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<List<CommentBean>>> response) {
                        DialogUtils.dismiss();
                        if ("0".equals(response.body().getCode())&&response.body().getData()!=null){
                            mView.onSetSubComment(response.body().getData());
                        } else {
                            mView.onGetSubCommentFailed(response.body().getMessage()==null?"获取评论失败":response.body().getMessage());
                        }
                    }
                });
    }

    @Override
    public void sendComment(String s) {
        OkGo.<BaseResponseBean<String>>post(ApiConstant.BASE_URL+ApiConstant.ADD_COMMENT)
                .upJson(s)
                .execute(new JsonCallback<BaseResponseBean<String>>() {
                    @Override
                    public void onSuccess(Response<BaseResponseBean<String>> response) {
                        if ("0".equals(response.body().getCode())){
                            mView.onSendCommentSuccess();
                        } else {
                            mView.onSendCommentFailed(response.body().getMessage()==null?"评论失败":response.body().getMessage());
                        }
                    }
                });
    }


}
